package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.pojo.*;
import com.guigu.www.service.FileService;
import com.guigu.www.service.MDesignProcedureDetailsService;
import com.guigu.www.service.MDesignProcedureService;
import com.guigu.www.service.ManufactureConfigProcedureListService;
import com.guigu.www.service.imp.IdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MDesignProcedureController {
    //产品生产工序
    @Autowired
    MDesignProcedureService mDesignProcedureService;

    //产品生产工序明细表
    @Autowired
    MDesignProcedureDetailsService mdpdservice2;

    @Autowired
    IdUtils idUtils;


    @Autowired
    FileService fileService;


    //工序表添加到明细表*
    @RequestMapping("adddesignProcedureshow.action")
    public Map adddesignProcedureshowwe(@RequestBody DesignProcedure designProcedure
    ) {
        //流水号
        designProcedure.setDesignid(idUtils.getid2(designProcedure));
        Map map = new HashMap();
        //生产表
        boolean f = mDesignProcedureService.save(designProcedure);
        //生产表明细表
        List<DesignProcedureDetails> list = designProcedure.getDetailss();
        for (DesignProcedureDetails details : list) {
            DesignProcedureDetails newdp = new DesignProcedureDetails();
            DesignProcedure p = mDesignProcedureService.getById(designProcedure.getId());
            newdp.setParentid(p.getId());
            newdp.setId(null);
            newdp.setProceduredescribe(details.getProceduredescribe());
            newdp.setRegister(details.getRegister());
            newdp.setRegistertime(details.getRegistertime());
            newdp.setDetailsnumber(1);
            newdp.setAmountunit("小时");
            newdp.setDesignmoduletag("D002-0");
            newdp.setDesignmodulechangetag("D003-0");
            newdp.setProcedurename(details.getProcedurename());
            newdp.setProcedureid(details.getProcedureid());
            newdp.setLabourhouramount(details.getLabourhouramount());
            newdp.setCostprice(details.getCostprice());
            newdp.setSubtotal(details.getSubtotal());
            mdpdservice2.save(newdp);
        }
        if (f) {
            map.put("msg", "制定成功,请复核");
            map.put("cm", "1");
        } else {
            map.put("msg", "制定失败");
        }
        return map;
    }



    //`adding`*
    @RequestMapping("readddesignProcedure.action")
    public Map readddesignProcedure(@RequestBody DesignProcedure designProcedure) {
        designProcedure.setDesignid(idUtils.getid2(designProcedure));
        Map map = new HashMap();
        //生产表
        boolean f = mDesignProcedureService.save(designProcedure);
        //生产表明细表
        List<DesignProcedureDetails> list = designProcedure.getDetailss();
        for (DesignProcedureDetails details : list) {
            DesignProcedureDetails newdp = new DesignProcedureDetails();
            DesignProcedure p = mDesignProcedureService.getById(designProcedure.getId());
            newdp.setParentid(p.getId());
            newdp.setId(null);
            newdp.setProceduredescribe(details.getProceduredescribe());
            newdp.setRegister(details.getRegister());
            newdp.setRegistertime(details.getRegistertime());
            newdp.setDetailsnumber(1);
            newdp.setAmountunit("小时");
            newdp.setDesignmoduletag("D002-0");
            newdp.setDesignmodulechangetag("D003-0");
            newdp.setProcedurename(details.getProcedurename());
            newdp.setProcedureid(details.getProcedureid());
            newdp.setLabourhouramount(details.getLabourhouramount());
            newdp.setCostprice(details.getCostprice());
            newdp.setSubtotal(details.getSubtotal());
            mdpdservice2.save(newdp);
        }
        if (f) {
            map.put("msg", "复核不通过，请重新制定");
            map.put("cm", "1");
        } else {
            map.put("msg", "失败");
        }
        return map;
    }


    //(序号)*
    @GetMapping("querydesignProcedure2.action/{id}")
    public DesignProcedure querymcpdesignProcedurebyids2(@PathVariable Integer id) {
        return mDesignProcedureService.getById(id);
    }



    //工序表修改和明细表*
    @RequestMapping("updateesignProcedureshow.action")
    public Map updateesignProcedureshowwe(@RequestBody DesignProcedure designProcedure
    ) {
        Map map = new HashMap();
        designProcedure.setChangetag("S001-1");
        boolean f = mDesignProcedureService.updateById(designProcedure);
        List<DesignProcedureDetails> list = designProcedure.getDetailss();
        for (DesignProcedureDetails details : list) {
            System.out.println(details);
            UpdateWrapper<DesignProcedureDetails> updateWrapper=new  UpdateWrapper<DesignProcedureDetails>();
            updateWrapper.set(" DETAILS_NUMBER",1)
                    .set("PROCEDURE_ID",details.getProcedureid())
                    .set("PROCEDURE_NAME",details.getProcedurename())
                    .set("LABOUR_HOUR_AMOUNT",details.getLabourhouramount())
                    .set("PROCEDURE_DESCRIBE",details.getProceduredescribe())
                    .set("AMOUNT_UNIT","小时")
                    .set("COST_PRICE",details.getCostprice())
                    .set("SUBTOTAL",details.getSubtotal())
                    .set("MODULE_SUBTOTAL",details.getModulesubtotal())
                    .set("REGISTER",details.getRegister())
                    .set("REGISTER_TIME",details.getRegistertime())
                    .set("REGISTER_TIME",details.getRegistertime())
                    .set("DESIGN_MODULE_TAG","D002-0")
                    .set("DESIGN_MODULE_CHANGE_TAG","D003-0")
                    .eq("id",designProcedure.getId());
                    mdpdservice2.update(new DesignProcedureDetails(),updateWrapper);
        }
        if (f) {
            map.put("msg", "审核成功");
            map.put("cm", "1");

        } else {
            map.put("msg", "审核失败");
        }
        return map;
    }

    //变更
    @RequestMapping("updateesignProcedureshowbiangeng1.action")
    public Map updateesignProcedureshowbiangeng1(@RequestBody DesignProcedure designProcedure
    ) {
        Map map = new HashMap();
        designProcedure.setChangetag("B002-1");
        designProcedure.setChecktag("S001-0");
        boolean f = mDesignProcedureService.updateById(designProcedure);
        List<DesignProcedureDetails> list = designProcedure.getDetailss();
        for (DesignProcedureDetails details : list) {
            UpdateWrapper<DesignProcedureDetails> updateWrapper=new  UpdateWrapper<DesignProcedureDetails>();
            updateWrapper.set(" DETAILS_NUMBER",1)
                    .set("PROCEDURE_ID",details.getProcedureid())
                    .set("PROCEDURE_NAME",details.getProcedurename())
                    .set("LABOUR_HOUR_AMOUNT",details.getLabourhouramount())
                    .set("PROCEDURE_DESCRIBE",details.getProceduredescribe())
                    .set("AMOUNT_UNIT","小时")
                    .set("COST_PRICE",details.getCostprice())
                    .set("SUBTOTAL",details.getSubtotal())
                    .set("MODULE_SUBTOTAL",details.getModulesubtotal())
                    .set("REGISTER",details.getRegister())
                    .set("REGISTER_TIME",details.getRegistertime())
                    .set("REGISTER_TIME",details.getRegistertime())
                    .set("DESIGN_MODULE_TAG","D002-0")
                    //已经变更
                    .set("DESIGN_MODULE_CHANGE_TAG","D003-1")
                    .eq("id",designProcedure.getId());
                    mdpdservice2.update(null,updateWrapper);
        }
        if (f) {
            map.put("msg", "提交成功");
            map.put("cm", "1");

        } else {
            map.put("msg", "提交失败");
        }
        return map;
    }


    //工序表修改和明细表*重复
    @RequestMapping("updateesignProcedureshowchongfu.action")
    public Map updateesignProcedureshowchongfu(@RequestBody DesignProcedure designProcedure
    ) {
        Map map = new HashMap();
        designProcedure.setChangetag("S001-3");
        boolean f = mDesignProcedureService.updateById(designProcedure);
        List<DesignProcedureDetails> list = designProcedure.getDetailss();
        for (DesignProcedureDetails details : list) {
            DesignProcedureDetails newdp = new DesignProcedureDetails();
            DesignProcedure p = mDesignProcedureService.getById(designProcedure.getId());
            newdp.setParentid(p.getId());
            newdp.setId(null);
            newdp.setProceduredescribe(details.getProceduredescribe());
            newdp.setRegister(details.getRegister());
            newdp.setRegistertime(details.getRegistertime());
            newdp.setDetailsnumber(1);
            newdp.setAmountunit("小时");
            newdp.setDesignmoduletag("D002-0");
            newdp.setDesignmodulechangetag("D003-0");
            newdp.setProcedurename(details.getProcedurename());
            newdp.setProcedureid(details.getProcedureid());
            newdp.setLabourhouramount(details.getLabourhouramount());
            newdp.setCostprice(details.getCostprice());
            newdp.setSubtotal(details.getSubtotal());
            mdpdservice2.updateById(newdp);
        }
        if (f) {
          //  map.put("msg", "审核成功");
            map.put("cm", "1");

        } else {
            map.put("msg", "审核失败");
        }
        return map;
    }



    //变更工序表添加到明细表*
    @RequestMapping("adddesignProcedureshowwe22.action")
    public Map adddesignProcedureshowwe22(@RequestBody DesignProcedure designProcedure
    ) {
        //流水号
        designProcedure.setDesignid(idUtils.getid2(designProcedure));
        Map map = new HashMap();
        boolean f = mDesignProcedureService.save(designProcedure);
        List<DesignProcedureDetails> list = designProcedure.getDetailss();
        for (DesignProcedureDetails details : list) {
            DesignProcedureDetails newdp = new DesignProcedureDetails();
            DesignProcedure p = mDesignProcedureService.getById(designProcedure.getId());
            newdp.setParentid(p.getId());
            newdp.setId(null);
            newdp.setProceduredescribe(details.getProceduredescribe());
            newdp.setRegister(details.getRegister());
            newdp.setRegistertime(details.getRegistertime());
            newdp.setDetailsnumber(1);
            newdp.setAmountunit("小时");
            newdp.setDesignmoduletag("D002-0");
            //变更标志
            newdp.setDesignmodulechangetag("D003-1");
            newdp.setProcedurename(details.getProcedurename());
            newdp.setProcedureid(details.getProcedureid());
            newdp.setLabourhouramount(details.getLabourhouramount());
            newdp.setCostprice(details.getCostprice());
            newdp.setSubtotal(details.getSubtotal());
            mdpdservice2.save(newdp);
        }
        if (f) {
            map.put("msg", "提交成功,请复核");
            map.put("cm", "1");
            //updatefile
        } else {
            map.put("msg", "提交失败");
        }
        return map;
    }


    //all*
    @RequestMapping("querydesignProcedureall.action")
    public PageVo<DesignProcedure> querydesignProcedureall(DesignProcedure designProcedure,
                                                           @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                           @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<DesignProcedure> queryWrapper = new QueryWrapper<DesignProcedure>();

        Page<DesignProcedure> page = mDesignProcedureService.page(new Page<DesignProcedure>(pagenum, pagesize), queryWrapper);

        PageVo<DesignProcedure> pageVo = new PageVo<DesignProcedure>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }


    //审核all*
    @RequestMapping("querydesignProceduretwo.action")
    public PageVo<DesignProcedure> querydesignProceduretwo(DesignProcedure designProcedure,
                                                           @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                           @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<DesignProcedure> queryWrapper = new QueryWrapper<DesignProcedure>();

        queryWrapper.eq("CHECK_TAG", "S001-0");


        Page<DesignProcedure> page = mDesignProcedureService.page(new Page<DesignProcedure>(pagenum, pagesize), queryWrapper);

        PageVo<DesignProcedure> pageVo = new PageVo<DesignProcedure>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }



    //变更*
    @RequestMapping("querydesignProcedurebiangeng.action")
    public PageVo<DesignProcedure> querydesignProcedurebiangeng(DesignProcedure designProcedure,
                                                                @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                                @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<DesignProcedure> queryWrapper = new QueryWrapper<DesignProcedure>();

        queryWrapper.eq("CHECK_TAG", "S001-1");


        Page<DesignProcedure> page = mDesignProcedureService.page(new Page<DesignProcedure>(pagenum, pagesize), queryWrapper);

        PageVo<DesignProcedure> pageVo = new PageVo<DesignProcedure>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }


    //dq表查询*
    @RequestMapping("querydesignProceduretwo3chaxu.action")
    public PageVo<DesignProcedure> querydesignProceduretwo3chaxu(DesignProcedure designProcedure,
                                                            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                            @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<DesignProcedure> queryWrapper = new QueryWrapper<DesignProcedure>();

        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DESIGN_MODULE_TAG", "D002-0");

        Page<DesignProcedure> page = mDesignProcedureService.page(new Page<DesignProcedure>(pagenum, pagesize), queryWrapper);

        PageVo<DesignProcedure> pageVo = new PageVo<DesignProcedure>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }



    //dq表审核*
    @RequestMapping("querydesignProceduretwo2.action")
    public PageVo<DesignProcedure> querydesignProceduretwo2(DesignProcedure designProcedure,
                                                            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                            @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<DesignProcedure> queryWrapper = new QueryWrapper<DesignProcedure>();

        queryWrapper.eq("DESIGN_MODULE_TAG","D002-1");

        Page<DesignProcedure> page = mDesignProcedureService.page(new Page<DesignProcedure>(pagenum, pagesize), queryWrapper);

        PageVo<DesignProcedure> pageVo = new PageVo<DesignProcedure>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }


    //dq表变更*
    @RequestMapping("querydesignProcedurebiangeng2.action")
    public PageVo<DesignProcedure> querydesignProcedurebiangeng2(DesignProcedure designProcedure,
                                                                 @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                                 @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<DesignProcedure> queryWrapper = new QueryWrapper<DesignProcedure>();

        queryWrapper.eq("CHECK_TAG", "S001-1");

        Page<DesignProcedure> page = mDesignProcedureService.page(new Page<DesignProcedure>(pagenum, pagesize), queryWrapper);

        PageVo<DesignProcedure> pageVo = new PageVo<DesignProcedure>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }



}
